import Vue from 'vue'
import Vuex from 'vuex'
//1.导入插件
import VuexPersistence from 'vuex-persist'
//2. 创建对象进行配置

//配置存储介质，默认持久化到本地存储中
const vuexLocal = new VuexPersistence({
  storage: window.localStorage,
})

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userName: "",
    user_id: "",
    addList: [],
    allNum: 0,
    cartData: [],
  },
  mutations: {
    user(state, index) {
      state.userName = index
    },
    delLogin(state) {
      state.userName = ""
    },
    setAddOrder(state, index) {
      state.addList = index
    },
    setCartData(state, data) {
      state.allNum = 0
      data.forEach(item => {
        item.money = item.num * item.price
        item.visible = false
        state.allNum += item.num
      })
      state.cartData = data
    },
  },
  actions: {},
  getters: {

  },
  modules: {},
  plugins: [vuexLocal.plugin],//引入插件
})